﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace T8task1
{
    public partial class GameListForm : Form
    {
        DataSet ds = new DataSet();
        private void ConnDatabase() 
        {
            string sql = "select * from [Categories];select * from [Products]";
            ds = DBHelper.GetDataSet(sql);
        }

        //显示游戏类别表数据
        private void ShowCategoryList() 
        {
            //获取游戏类别数据
            DataTable dt = ds.Tables[0];

            //创建根节点
            TreeNode root = new TreeNode("游戏类别");
            root.Tag = 0;//保存编号

            //将根节点连接到TreeView控件上
            this.tvCategory.Nodes.Add(root);

            //循环获取每行类别数据并创建为树节点
            foreach (DataRow row in dt.Rows)
            {
                string name = Convert.ToString(row["Name"]);

                //创建新类别节点
                TreeNode node = new TreeNode(name);

                //保存游戏类别编号
                node.Tag = Convert.ToInt32(row["ID"]);
                root.Nodes.Add(node);
            }
            //展开treeview控件上的所有节点
            this.tvCategory.ExpandAll();

        }

        //显示游戏列表
        private void ShowGameList() 
        {
            //获取游戏数据
            DataTable dt = ds.Tables[1];
            this.dgvGame.AutoGenerateColumns = false;
            this.dgvGame.DataSource = dt;
        }
        public GameListForm()
        {
            InitializeComponent();
        }

        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //连接数据库填充数据集
            ConnDatabase();
            ShowCategoryList();
            ShowGameList();
        }

        private void tvCategory_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //获取选中的树节点
            TreeNode node = e.Node;
            //获取类别的编号（ID）
            int id = Convert.ToInt32(node.Tag);
            if (id != 0) 
            {
                DataView dv = ds.Tables[1].DefaultView;
                //根据类别ID筛选游戏数据
                dv.RowFilter = string.Format("Categoryid={0}",id);
                this.dgvGame.DataSource = dv;
            }

        }
    }
}
